<template>
	<view class="menu-box left clearfix">
		<view class="menu-breadcrumb left clearfix">
			<uni-breadcrumb separator="/" style="width:300px;float:left;">
			  <uni-breadcrumb-item v-for="(route,index) in routes" :key="index" :to="route.to">
			    {{route.name}}
			  </uni-breadcrumb-item>
			</uni-breadcrumb>
			
		</view>
		
		<!--内容开始-->
		<view class="add-box left clearfix">
			
				<view class="example">
					<!-- 基础用法，不包含校验规则 -->
					<uni-forms ref="form" :modelValue="formData" label-width="50">
						
						<uni-forms-item label="中文介绍" required name="content">
							<zmt-editor :initialContent="zhContent" @contentChange="getZhContent"></zmt-editor>
						</uni-forms-item>
						<uni-forms-item label="英文介绍" required name="en_content">
							<zmt-editor :initialContent="enContent" @contentChange="getEnContent"></zmt-editor>
						</uni-forms-item>
						
						
						<uni-forms-item label="" style="margin-top:50px;text-align: center;">
							
							<view class="form-quxiao-submit right" @click="navigatorToList()">
								取消
							</view>
							
							<view class="form-submit right" @click="submitForm">
								发布
							</view>
							
						</uni-forms-item>
						
					</uni-forms>
				</view>
		</view>
		<!--内容结束-->
		
		
	</view>
</template>
<style scoped>
	.add-null-image-box{width:100%;height:300px;line-height:300px;text-align: center;font-size:14px;color:#999;}
	.add-image-box{width:230px;height:150px;border:1px solid #eee;border-radius: 5px;margin:4px;cursor:pointer;overflow: hidden;}
	.add-select-content{width:96%;margin-left:2%;margin-top:20px;height:420px;overflow: auto;}
	.add-select-title label{float:right;margin-right:10px;cursor:pointer;}
	.add-select-title{width:calc(100% - 10px);padding-left:10px;line-height:35px;font-size:13px;color:#555;}
	.add-select-box{width:1000px;height:500px;background:#fff;border-radius: 10px;}
	.poster-box{width:300px;height:200px;border:1px solid #eee;border-radius: 5px;background:#f8f8f8;line-height:180px;text-align: center;}
	.form-quxiao-submit{width:100px;line-height:40px;height:40px;color:#666;border-radius: 5px;text-align: center;border:1px solid #ccc;margin-right:20px;cursor:pointer;}
	.form-submit{width:100px;line-height:40px;height:40px;background:#2979ff;color:#fff;border-radius: 5px;text-align: center;margin-right:20px;cursor:pointer;}
	.example {
			padding: 15px;
			background-color: #fff;
		}
	
		.segmented-control {
			margin-bottom: 15px;
		}
	
		.button-group {
			margin-top: 15px;
			display: flex;
			justify-content: space-around;
		}
	
		.form-item {
			display: flex;
			align-items: center;
		}
	
		.button {
			display: flex;
			align-items: center;
			height: 35px;
			margin-left: 10px;
		}
	.add-box{width:96%;margin-left:2%;margin-top:30px;min-height:400px;}
</style>

<script>
	var db = uniCloud.database();
	var systemMenu = uniCloud.importObject('systemmenu',{customUI:true});
	var aliyun = uniCloud.importObject('aliyun',{customUI:true});
	import common from '/static/js/common.js';
	export default {
		name:"news-add",
		data() {
			return {
				zhContent:'',//中文内容
				enContent:'',//英文内容
				image:[],
				parentTitle:null,
				formData:{
					content:'',
					en_content:''
				},
				routes: [
				        {
				          to: "/A",
				          name: "官网管理",
				        },
				        {
				          to: "/B",
				          name: "企业介绍",
				        },
				      ],
					rules: {
						
						content:{
							rules: [
								{
								required: true,
								errorMessage: '请输入中文企业介绍'
								}
							]
						},
						en_content:{
							rules: [
								{
								required: true,
								errorMessage: '请输入英文企业介绍'
								}
							]
						}
					}
			};
		},
		props:{
			id:{
				type:String
			},
			userinfo:{
				type:Object,
				title:'用户信息'
			}
		},
		mounted() {
				var that = this;
				that.getDefaultData();
			},
		methods:{
			//获取默认值
			getDefaultData(){
				var that = this;
				db.collection('article').where({type:'aboutus'}).get({getOne:true}).then(res=>{
					var data = res.result.data;
					
					if(data && data._id){
						that.formData = data;
						that.enContent = data.en_content;
						that.zhContent = data.content;
					}
					
				});
			},
			//设置内容
			getZhContent(e){
				this.formData.content = e;
			},
			getEnContent(e){
				this.formData.en_content = e;
			},
			//监听富文本内容改变
			        editorChange(e){
			            this.form.desc = e
			        },
			
			
			
			//提交
			submitForm(form){
				var that = this;
				this.$refs.form.validate().then(res=>{
					uni.showLoading({
						title:'添加中'
					})
					
					if(this.formData){
						var id = this.formData._id;
					}
					
					res.type='aboutus';
					if(!id || id=='undefined'){
							res.uid = that.userinfo._id;
							db.collection('article').add(res).then(result=>{
								uni.hideLoading()
								if(result.result.id){
									this.formData._id = result.result.id;
									uni.showToast({
										title:'添加成功',
										icon:'none',
										duration:2000
									})
									
								}
							}).catch(err=>{
								uni.hideLoading()
							});
						
					}else{
							//修改子菜单
								db.collection('article').where({_id:id}).update(res).then(result=>{
									uni.showToast({
										title:'修改成功',
										icon:'none',
										duration:2000
									})
								})
							
						
					}
					
				})
			}
		}
	}
</script>

